#[derive(Debug)]
#[repr(u16)]
pub enum Error {
    Errno(u16) = 0,
    Kernel(u16) = 1,
    TaskLib(u16) = 3,
    DbgLib(u16) = 4,
    VxdbgLib(u16) = 5,
    DsmLib(u16) = 7,
    FioLib(u16) = 9,
    IoLib(u16) = 12,
    IosLib(u16) = 13,
    LoadLib(u16) = 14,
    LstLib(u16) = 15,
    MemLib(u16) = 17,
    RngLib(u16) = 19,
    SemLib(u16) = 22,
    VwModNum(u16) = 27,
    SymLib(u16) = 28,
    TyLib(u16) = 31,
    WdLib(u16) = 34,
    UsrLib(u16) = 35,
    RemLib(u16) = 37,
    NetDrv(u16) = 41,
    InetLib(u16) = 43,
    RouteLib(u16) = 44,
    NfsDrv(u16) = 45,
    NfsLib(u16) = 46,
    RpcClntStat(u16) = 47,
    NfsStat(u16) = 48,
    ErrnoLib(u16) = 49,
    HostLib(u16) = 50,
    SigLib(u16) = 51,
    FtnLib(u16) = 52,
    IfSl(u16) = 53,
    LoginLib(u16) = 54,
    ScsiLib(u16) = 55,
    DosFsLib(u16) = 56,
    SelectLib(u16) = 57,
    HashLib(u16) = 58,
    QLib(u16) = 59,
    TickLib(u16) = 60,
    ObjLib(ObjLibError) = 61,
    QPriHeapLib(u16) = 62,
    QPriBMapLib(u16) = 63,
    BufLib(u16) = 64,
    MsgQLib(u16) = 65,
    ClassLib(u16) = 66,
    IntLib(u16) = 67,
    DirLib(u16) = 68,
    CacheLib(u16) = 69,
    RawFsLib(u16) = 70,
    ArpLib(u16) = 71,
    SmLib(u16) = 72,
    BootpLib(u16) = 73,
    IcmpLib(u16) = 74,
    TftpLib(u16) = 75,
    ProxyArpLib(u16) = 76,
    SmUtilLib(u16) = 77,
    SmPktLib(u16) = 78,
    LoadEcoffLib(u16) = 79,
    LoadAoutLib(u16) = 80,
    LoadBoutLib(u16) = 81,
    BootLoadLib(u16) = 82,
    LoadCoffLib(u16) = 83,
    VmLib(u16) = 84,
    MmuLib(u16) = 85,
    ModuleLib(u16) = 86,
    UnldLib(u16) = 87,
    SmObjLib(u16) = 88,
    SmNameLib(u16) = 89,
    CplusLib(u16) = 90,
    M2Lib(u16) = 91,
    AioPxLib(u16) = 92,
    LoadAoutHppaLib(u16) = 93,
    MountLib(u16) = 94,
    NfsdLib(u16) = 95,
    LoadSomCoffLib(u16) = 96,
    LoadElfLib(u16) = 97,
    MbufLib(u16) = 98,
    PingLib(u16) = 99,
    StrmLib(u16) = 100,
    PppSecretLib(u16) = 101,
    PppHookLib(u16) = 102,
    TapeFsLib(u16) = 103,
    SnmpdLib(u16) = 104,
    PcmciaLib(u16) = 105,
    DhcpcLib(u16) = 106,
    ResolvLib(u16) = 107,
    EndLib(u16) = 108,
    MuxLib(u16) = 109,
    M2RipLib(u16) = 110,
    OspfLib(u16) = 111,
    DhcpsLib(u16) = 112,
    SntpcLib(u16) = 113,
    SntpsLib(u16) = 114,
    NetBufLib(u16) = 115,
    CdromFsLib(u16) = 116,
    LoadPecoffLib(u16) = 117,
    DistLib(u16) = 118,
    DistNameLib(u16) = 119,
    MsgQDistGrpLib(u16) = 120,
    MsgQDistLib(u16) = 121,
    IfUl(u16) = 122,
    MiiLib(u16) = 123,
    PoolLib(u16) = 124,
    SetLib(u16) = 125,
    DmsLib(u16) = 126,
    RarpLib(u16) = 127,
    IgmpRouterLib(u16) = 128,
    DevCfgLib(u16) = 129,
    QPriMaskLib(u16) = 130,
    UsbHostLib(u16) = 131,
    UsbPeriphLib(u16) = 132,
    CbioLib(u16) = 133,
    EventLib(u16) = 134,
    FastPathLib(u16) = 135,
    FtpLib(u16) = 136,
    FastUdpLib(u16) = 137,
    EdrLib(u16) = 138,
    PmLib(u16) = 139,
    MemLogLib(u16) = 140,
    IpRouteLib(u16) = 141,
    Nfs3Stat(u16) = 142,
    AlarmLib(u16) = 153,
    AmsLib(u16) = 154,
    FmsLib(u16) = 155,
    HsiLib(u16) = 156,
    HsmsLib(u16) = 157,
    OmsLib(u16) = 158,
    RpmLib(u16) = 159,
    UmsLib(u16) = 160,
    CmsLib(u16) = 161,
    MmsLib(u16) = 162,
    PspLib(u16) = 163,
    XcomLib(u16) = 164,
    IpsLib(u16) = 165,
    RdsLib(u16) = 166,
    GrmLib(u16) = 167,
    MtpLib(u16) = 168,
    ExEngLib(u16) = 169,
    TplLib(u16) = 170,
    SmmLib(u16) = 171,
    GraLib(u16) = 172,
    ShellLib(u16) = 173,
    IsrLib(u16) = 175,
    RtpLib(u16) = 178,
    CoprocLib(u16) = 179,
    AdrSpaceLib(u16) = 180,
    PgPoolLib(u16) = 181,
    PgPoolPhysLib(u16) = 182,
    PgPoolVirtLib(u16) = 183,
    SyscallLib(u16) = 184,
    PgMgrLib(u16) = 185,
    LoadRtpLib(u16) = 186,
    HookLib(u16) = 187,
    UnLib(u16) = 188,
    SalLib(u16) = 189,
    SdLib(u16) = 190,
    ShlLib(u16) = 191,
    TlsLib(u16) = 192,
    LoadFileLib(u16) = 193,
    ExcLib(u16) = 194,
    HrfsLib(u16) = 195,
    ErfLib(u16) = 196,
    CoreDumpLib(u16) = 197,
    TaskPxLib(u16) = 198,
    Ipcom(u16) = 199,
    Dshm(u16) = 200,
    SpinLockLib(u16) = 201,
    VxCpuLib(u16) = 202,
    TffsLib(u16) = 203,
    DevMemLib(u16) = 204,
    PwrPolicyLib(u16) = 205,
    FtlLib(u16) = 206,
    CondVarLib(u16) = 207,
    UdbMgr(u16) = 208,
    UserIdentLib(u16) = 209,
    Hypervisor(u16) = 210,
    PrivilegesLib(u16) = 211,
    RtpPart(u16) = 212,
    PppLib(u16) = 213,
    SshLib(u16) = 214,
    MsgQGblLib(u16) = 216,
    ApexObjLib(u16) = 217,
    PartitionLib(u16) = 218,
    HmLib(u16) = 219,
    PayloadLib(u16) = 220,
    ConfigRecordLib(u16) = 221,
    SslLib(u16) = 222,
    Vxcontainer(u16) = 223,
    VxcMgrLib(u16) = 224,

    Unknown(u16),
}

impl From<i32> for Error {
    fn from(x: i32) -> Self {
        let lib: u16 = (x >> 16) as u16;
        let err: u16 = (x & 0xFFFF) as u16;
        match lib {
            0 => Self::Errno(err), /* THIS MUST BE ZERO! */
            1 => Self::Kernel(err),
            3 => Self::TaskLib(err),
            4 => Self::DbgLib(err),
            5 => Self::VxdbgLib(err),
            7 => Self::DsmLib(err),
            9 => Self::FioLib(err),
            12 => Self::IoLib(err),
            13 => Self::IosLib(err),
            14 => Self::LoadLib(err),
            15 => Self::LstLib(err),
            17 => Self::MemLib(err),
            19 => Self::RngLib(err),
            22 => Self::SemLib(err),
            27 => Self::VwModNum(err),
            28 => Self::SymLib(err),
            31 => Self::TyLib(err),
            34 => Self::WdLib(err),
            35 => Self::UsrLib(err),
            37 => Self::RemLib(err),
            41 => Self::NetDrv(err),
            43 => Self::InetLib(err),
            44 => Self::RouteLib(err),
            45 => Self::NfsDrv(err),
            46 => Self::NfsLib(err),
            47 => Self::RpcClntStat(err),
            48 => Self::NfsStat(err),
            49 => Self::ErrnoLib(err),
            50 => Self::HostLib(err),
            51 => Self::SigLib(err),
            52 => Self::FtnLib(err),
            53 => Self::IfSl(err),
            54 => Self::LoginLib(err),
            55 => Self::ScsiLib(err),
            56 => Self::DosFsLib(err),
            57 => Self::SelectLib(err),
            58 => Self::HashLib(err),
            59 => Self::QLib(err),
            60 => Self::TickLib(err),
            61 => Self::ObjLib(err.into()),
            62 => Self::QPriHeapLib(err),
            63 => Self::QPriBMapLib(err),
            64 => Self::BufLib(err),
            65 => Self::MsgQLib(err),
            66 => Self::ClassLib(err),
            67 => Self::IntLib(err),
            68 => Self::DirLib(err),
            69 => Self::CacheLib(err),
            70 => Self::RawFsLib(err),
            71 => Self::ArpLib(err),
            72 => Self::SmLib(err),
            73 => Self::BootpLib(err),
            74 => Self::IcmpLib(err),
            75 => Self::TftpLib(err),
            76 => Self::ProxyArpLib(err),
            77 => Self::SmUtilLib(err),
            78 => Self::SmPktLib(err),
            79 => Self::LoadEcoffLib(err),
            80 => Self::LoadAoutLib(err),
            81 => Self::LoadBoutLib(err),
            82 => Self::BootLoadLib(err),
            83 => Self::LoadCoffLib(err),
            84 => Self::VmLib(err),
            85 => Self::MmuLib(err),
            86 => Self::ModuleLib(err),
            87 => Self::UnldLib(err),
            88 => Self::SmObjLib(err),
            89 => Self::SmNameLib(err),
            90 => Self::CplusLib(err),
            91 => Self::M2Lib(err),
            92 => Self::AioPxLib(err),
            93 => Self::LoadAoutHppaLib(err),
            94 => Self::MountLib(err),
            95 => Self::NfsdLib(err),
            96 => Self::LoadSomCoffLib(err),
            97 => Self::LoadElfLib(err),
            98 => Self::MbufLib(err),
            99 => Self::PingLib(err),
            100 => Self::StrmLib(err),
            101 => Self::PppSecretLib(err),
            102 => Self::PppHookLib(err),
            103 => Self::TapeFsLib(err),
            104 => Self::SnmpdLib(err),
            105 => Self::PcmciaLib(err),
            106 => Self::DhcpcLib(err),
            107 => Self::ResolvLib(err),
            108 => Self::EndLib(err),
            109 => Self::MuxLib(err),
            110 => Self::M2RipLib(err),
            111 => Self::OspfLib(err),
            112 => Self::DhcpsLib(err),
            113 => Self::SntpcLib(err),
            114 => Self::SntpsLib(err),
            115 => Self::NetBufLib(err),
            116 => Self::CdromFsLib(err),
            117 => Self::LoadPecoffLib(err),
            118 => Self::DistLib(err),
            119 => Self::DistNameLib(err),
            120 => Self::MsgQDistGrpLib(err),
            121 => Self::MsgQDistLib(err),
            122 => Self::IfUl(err),
            123 => Self::MiiLib(err),
            124 => Self::PoolLib(err),
            125 => Self::SetLib(err),
            126 => Self::DmsLib(err),
            127 => Self::RarpLib(err),
            128 => Self::IgmpRouterLib(err),
            129 => Self::DevCfgLib(err),
            130 => Self::QPriMaskLib(err),
            131 => Self::UsbHostLib(err),
            132 => Self::UsbPeriphLib(err),
            133 => Self::CbioLib(err),
            134 => Self::EventLib(err),
            135 => Self::FastPathLib(err),
            136 => Self::FtpLib(err),
            137 => Self::FastUdpLib(err),
            138 => Self::EdrLib(err),
            139 => Self::PmLib(err),
            140 => Self::MemLogLib(err),
            141 => Self::IpRouteLib(err),
            142 => Self::Nfs3Stat(err),
            153 => Self::AlarmLib(err),
            154 => Self::AmsLib(err),
            155 => Self::FmsLib(err),
            156 => Self::HsiLib(err),
            157 => Self::HsmsLib(err),
            158 => Self::OmsLib(err),
            159 => Self::RpmLib(err),
            160 => Self::UmsLib(err),
            161 => Self::CmsLib(err),
            162 => Self::MmsLib(err),
            163 => Self::PspLib(err),
            164 => Self::XcomLib(err),
            165 => Self::IpsLib(err),
            166 => Self::RdsLib(err),
            167 => Self::GrmLib(err),
            168 => Self::MtpLib(err),
            169 => Self::ExEngLib(err),
            170 => Self::TplLib(err),
            171 => Self::SmmLib(err),
            172 => Self::GraLib(err),
            173 => Self::ShellLib(err),
            175 => Self::IsrLib(err),
            178 => Self::RtpLib(err),
            179 => Self::CoprocLib(err),
            180 => Self::AdrSpaceLib(err),
            181 => Self::PgPoolLib(err),
            182 => Self::PgPoolPhysLib(err),
            183 => Self::PgPoolVirtLib(err),
            184 => Self::SyscallLib(err),
            185 => Self::PgMgrLib(err),
            186 => Self::LoadRtpLib(err),
            187 => Self::HookLib(err),
            188 => Self::UnLib(err),
            189 => Self::SalLib(err),
            190 => Self::SdLib(err),
            191 => Self::ShlLib(err),
            192 => Self::TlsLib(err),
            193 => Self::LoadFileLib(err),
            194 => Self::ExcLib(err),
            195 => Self::HrfsLib(err),
            196 => Self::ErfLib(err),
            197 => Self::CoreDumpLib(err),
            198 => Self::TaskPxLib(err),
            199 => Self::Ipcom(err),
            200 => Self::Dshm(err),
            201 => Self::SpinLockLib(err),
            202 => Self::VxCpuLib(err),
            203 => Self::TffsLib(err),
            204 => Self::DevMemLib(err),
            205 => Self::PwrPolicyLib(err),
            206 => Self::FtlLib(err),
            207 => Self::CondVarLib(err),
            208 => Self::UdbMgr(err),
            209 => Self::UserIdentLib(err),
            210 => Self::Hypervisor(err),
            211 => Self::PrivilegesLib(err),
            212 => Self::RtpPart(err),
            213 => Self::PppLib(err),
            214 => Self::SshLib(err),
            216 => Self::MsgQGblLib(err),
            217 => Self::ApexObjLib(err),
            218 => Self::PartitionLib(err),
            219 => Self::HmLib(err),
            220 => Self::PayloadLib(err),
            221 => Self::ConfigRecordLib(err),
            222 => Self::SslLib(err),
            223 => Self::Vxcontainer(err),
            224 => Self::VxcMgrLib(err),
            _ => Self::Unknown(err),
        }
    }
}

#[derive(Debug)]
pub enum ObjLibError {
    IdError,
    Unavailable,
    Deleted,
    Timeout,
    NoMethod,
    NoAccessRights,
    NameTruncated,
    NameClash,
    NotNamed,
    IllegalClassType,
    OperationUnsupported,
    InvalidOwner,
    RenameNotAllowed,
    DestroyError,
    HandleTblFull,
    NotFound,
    InvalidArgument,
    LibNotInitialized,
    AccessDenied,
    Unknown(u16),
}

impl From<u16> for ObjLibError {
    fn from(value: u16) -> Self {
        match value {
            0x01 => ObjLibError::IdError,
            0x02 => ObjLibError::Unavailable,
            0x03 => ObjLibError::Deleted,
            0x04 => ObjLibError::Timeout,
            0x05 => ObjLibError::NoMethod,
            0x06 => ObjLibError::NoAccessRights,
            0x07 => ObjLibError::NameTruncated,
            0x08 => ObjLibError::NameClash,
            0x09 => ObjLibError::NotNamed,
            0x0A => ObjLibError::IllegalClassType,
            0x0B => ObjLibError::OperationUnsupported,
            0x0C => ObjLibError::InvalidOwner,
            0x0D => ObjLibError::RenameNotAllowed,
            0x0E => ObjLibError::DestroyError,
            0x0F => ObjLibError::HandleTblFull,
            0x10 => ObjLibError::NotFound,
            0x11 => ObjLibError::InvalidArgument,
            0x12 => ObjLibError::LibNotInitialized,
            0x13 => ObjLibError::AccessDenied,
            _ => Self::Unknown(value),
        }
    }
}
